// pages/index-setting/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    deviceList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    app.api.getDevice().then(res => {
      if (res.data != null && res.data instanceof Array) {
        let value = wx.getStorageSync('deviceList')
        if (value == null || value == "" || value == undefined) {
          let _list = []
          for (let i = 1; i < res.data.length + 1; i++) {
            let item = res.data[i - 1]
            if (item != null) {
              _list.push({
                sort: i,
                image: item.type === '1' ? 'http://www.emodule.com.cn:4443/public/img/light.png' : item.type === '2' ?
                  'http://www.emodule.com.cn:4443/public/img/cable.png' : item.type === '3' ? 'http://www.emodule.com.cn:4443/public/img/wind-fan.png' : item.type === '9' ? 'http://www.emodule.com.cn:4443/public/img/room.png' : item.type === '10' ? 'http://www.emodule.com.cn:4443/public/img/cable.png' : item.type === '12' ? 'http://www.emodule.com.cn:4443/public/img/energy.png' : '',
                deviceName: item.deviceName,
                position: item.position,
                deviceId: item.deviceId,
                show: 1
              })
            }
          }
          wx.setStorageSync('deviceList', JSON.stringify(_list))
          this.setData({
            deviceList: _list
          })
        } else {
          let deviceList = JSON.parse(value);
          if (deviceList instanceof Array) {
            let _l = [];
            let _tempList = [];
            for (let i = 0; i < res.data.length; i++) {
              let item = res.data[i]
              if (item != null) {
                let index = _l.length;
                for (let j = 0; j < deviceList.length; j++) {
                  let t = deviceList[j];
                  if (t.deviceId == item.deviceId) {
                    let obj = {
                      sort: t.sort,
                      image: item.type === '1' ? 'http://www.emodule.com.cn:4443/public/img/light.png' : item.type === '2' ?
                        'http://www.emodule.com.cn:4443/public/img/cable.png' : item.type === '3' ? 'http://www.emodule.com.cn:4443/public/img/wind-fan.png' : item.type === '9' ? 'http://www.emodule.com.cn:4443/public/img/room.png' : item.type === '10' ? 'http://www.emodule.com.cn:4443/public/img/cable.png' : item.type === '12' ? 'http://www.emodule.com.cn:4443/public/img/energy.png' : '',
                      deviceName: item.deviceName,
                      position: item.position,
                      deviceId: item.deviceId,
                      show: t.show
                    }
                    _l.splice(t.sort - 1, 0, obj)
                  }
                }
                if (index == _l.length) {
                  _tempList.push(item)
                }
              }
            }
            let newList = _l.concat(_tempList)
            this.setData({
              deviceList: newList
            })
          }
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  handleShow(e) {
    let index = e.target.dataset.index
    let device = this.data.deviceList[index]
    device.show = device.show == 1 ? 0 : 1
    let _newList = this.data.deviceList
    _newList[index] = device
    this.setData({
      deviceList: _newList
    })
    wx.setStorageSync('deviceList', JSON.stringify(_newList))
  },
  moveUp(e) {
    let index = e.target.dataset.index
    let _list = this.data.deviceList
    _list = this.swapArr(_list, index - 1, index)
    let _newList = []
    _list.forEach((item, index) => {
      item.sort = index + 1
      _newList.push(item)
    })
    this.setData({
      deviceList: _newList
    })
    wx.setStorageSync('deviceList', JSON.stringify(_newList))
  },
  moveTop(e) {
    let index = e.target.dataset.index
    let _list = this.data.deviceList
    _list = this.toFirst(_list, index)
    let _newList = []
    _list.forEach((item, index) => {
      item.sort = index + 1
      _newList.push(item)
    })
    this.setData({
      deviceList: _newList
    })
    wx.setStorageSync('deviceList', JSON.stringify(_newList))
  },
  swapArr(arr, index1, index2) {
    arr[index1] = arr.splice(index2, 1, arr[index1])[0];
    return arr;
  },
  toFirst(fieldData, index) {
    if (index != 0) {
      // fieldData[index] = fieldData.splice(0, 1, fieldData[index])[0]; 这种方法是与另一个元素交换了位子，
      fieldData.unshift(fieldData.splice(index, 1)[0]);
    }
    return fieldData
  }
})